<template>
	<div class="wrapper">
				
				<!-- header部分 -->
				<header>
					<p>商品管理</p>
					<BackButton />
				</header>
				
				<!-- 商家logo部分 -->
				<div class="business-logo">
					<img :src="getImg(business.businessImg)">
				</div>
				<div class="business-info">
					<h1>{{business.businessName}}</h1>
					<p>&#165;{{business.starPrice}}起送 &#165;{{business.deliveryPrice}}配送</p>
					<p>{{business.businessExplain}}</p>
				</div>

				<div class="add-food-button">
					<button @click="tocreate(businessId)">新建商品</button>
				</div>
				<ul class="food">
					<li v-for="item in foodArr" :key="item.foodId">
						<div class="food-left">
							<img :src="getImg(item.foodImg)">
							<div class="food-left-info">
								<h3>{{item.foodName}}</h3>
								<p>{{item.foodExplain}}</p>
								<p>&#165;{{item.foodPrice}}</p>
							</div>
						</div>
						<div class="food-right">
							<button class="fooddelete" @click="todelete(item.foodId)">删除</button>
							<button class="foodchange" @click="tochange(item.foodId)">编辑</button>
						</div>
					</li>
				</ul>
	</div>
</template>

<script>
	import BackButton from '../components/BackButton.vue';
	export default{
		name:'BusinessControl',
		data(){
			return {
				businessId: this.$route.query.businessId,
				business:{},
				foodArr:[],
				user:{},
				isCartExpanded: false, // 购物车是否展开
				selectedItems: [],     // 已选商品
			}
		},
		
		created() {
			this.user = this.$getSessionStorage('user');
			console.log(this.user);
			//根据businessId查询商家信息
			this.$axios.post('BusinessController/getBusinessById',this.$qs.stringify({
				businessId:this.user.userId
			})).then(response=>{
				this.business = response.data;
			}).catch(error=>{
				console.error(error);
			});
			
			//根据businessId查询所属食品信息
			this.$axios.post('FoodController/listFoodByBusinessId',this.$qs.stringify({
				businessId:this.user.userId
			})).then(response=>{
				this.foodArr = response.data;
			}).catch(error=>{
				console.error(error);
			});
		},
		components:{
			BackButton
		},
		methods:{
			getImg(path){
				return 'http://47.92.162.176:8080'+path;
			},
			todelete(foodId){
				this.$axios.post('FoodController/delFoodById',this.$qs.stringify({
					foodId: foodId,
					businessId:this.user.userId
				})).then(response=>{
					if(response.data.code=='200'){
						alert("删除成功!");
					}else{
						alert("删除失败！");
					}
					window.location.reload(true);
				}).catch(error=>{
					console.error(error);
				});
			},
			tochange(foodId){
				this.$router.push({path:'/foodChange',query:{foodId:foodId}});
			},
			tocreate(businessId){
				this.$router.push({path:'/foodCreate',query:{businessId:businessId}});
			}

		},
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		min-height: 150vh;
		display: flex;
		flex-direction: column;
	}
	.content {
		flex: 1;
		padding-bottom: 14vw; /*设置与 footer 高度相同的底部内边距 */
	}
	.wrapper header{
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;
		
		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;
		
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.wrapper .business-logo{
		width: 100%;
		height: 35vw;
		/*使用上外边距避开header部分*/
		margin-top: 12vw;
		
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.wrapper .business-logo img{
		width: 40vw;
		height: 30vw;
		border-radius: 5px;
	}
	.wrapper .business-info{
		width: 100%;
		height: 20vw;
		
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.wrapper .business-info h1{
		font-size: 5vw;
	}
	.wrapper .business-info p{
		font-size: 3vw;
		color: #666;
		margin-top: 1vw;
	}
	.add-food-button {
		margin-top: 12vw; /* 与 header 下面留出空间 */
		padding: 1vw;
		display: flex;
		justify-content: center;
	}
	
	.add-food-button button {
		padding: 1vw 2vw;
		font-size: 4vw;
		background-color: #0097FF;
		color: white;
		border: none;
		border-radius: 5px;
		cursor: pointer;
	}
	.wrapper .food{
		width: 100%;
		/*使用下外边距避开footer部分*/
		margin-bottom: 14vw;
		padding-bottom: 6vw;
	}
	.wrapper .food li{
		width: 100%;
		box-sizing: border-box;
		padding: 2.5vw;
		user-select: none;
		
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.wrapper .food li .food-left{
		display: flex;
		align-items: center;
	}
	.wrapper .food li .food-left img{
		width: 20vw;
		height: 20vw;
	}
	.wrapper .food li .food-left .food-left-info{
		margin-left: 3vw;
	}
	.wrapper .food li .food-left .food-left-info h3{
		font-size: 3.8vw;
		color: #555;
	}
	.wrapper .food li .food-left .food-left-info p{
		font-size: 3vw;
		color: #888;
		margin-top: 2vw;
	}
	.wrapper .food li .food-right{
		width: 16vw;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.wrapper .food li .food-right .fa-minus-circle{
		font-size: 5.5vw;
		color: #999;
		cursor: pointer;
	}
	.wrapper .food li .food-right p{
		font-size: 3.6vw;
		color: #333;
	}
	.wrapper .food li .food-right .fa-plus-circle{
		font-size: 5.5vw;
		color: #0097EF;
		cursor: pointer;
	}
	.fooddelete {
		width: 10vw;
		height: 5vw;
		margin: 0 auto;
		font-size: 3vw;
		background-color: #0097FF;
		border: 1px solid;
		text-align: center;
	}
	.foodchange {
		width: 10vw;
		height: 5vw;
		margin: 0 auto;
		font-size: 3vw;
		background-color: #0097FF;
		border: 1px solid;
		text-align: center;
	}
	/*
	.wrapper .cart .cart-right .cart-right-item{
		width: 100%;
		height: 100%;
		background-color: #535356;
		color: #fff;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;
		
		display: flex;
		justify-content: center;
		align-items: center;
	}
	*/
</style>
